package cn.iocoder.yudao.module.ao.controller.admin.majordirection;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ao.controller.admin.majordirection.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.majordirection.MajorDirectionDO;
import cn.iocoder.yudao.module.ao.service.majordirection.MajorDirectionService;

@Tag(name = "管理后台 - 专业方向")
@RestController
@RequestMapping("/ao/major-direction")
@Validated
public class MajorDirectionController {

    @Resource
    private MajorDirectionService majorDirectionService;

    @PostMapping("/create")
    @Operation(summary = "创建专业方向")
    @PreAuthorize("@ss.hasPermission('ao:major-direction:create')")
    public CommonResult<Long> createMajorDirection(@Valid @RequestBody MajorDirectionSaveReqVO createReqVO) {
        return success(majorDirectionService.createMajorDirection(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专业方向")
    @PreAuthorize("@ss.hasPermission('ao:major-direction:update')")
    public CommonResult<Boolean> updateMajorDirection(@Valid @RequestBody MajorDirectionSaveReqVO updateReqVO) {
        majorDirectionService.updateMajorDirection(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专业方向")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ao:major-direction:delete')")
    public CommonResult<Boolean> deleteMajorDirection(@RequestParam("id") Long id) {
        majorDirectionService.deleteMajorDirection(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得专业方向")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ao:major-direction:query')")
    public CommonResult<MajorDirectionRespVO> getMajorDirection(@RequestParam("id") Long id) {
        MajorDirectionDO majorDirection = majorDirectionService.getMajorDirection(id);
        return success(BeanUtils.toBean(majorDirection, MajorDirectionRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得专业方向列表")
    @PreAuthorize("@ss.hasPermission('ao:major-direction:query')")
    public CommonResult<List<MajorDirectionRespVO>> getMajorDirectionList(@Valid MajorDirectionListReqVO listReqVO) {
        List<MajorDirectionDO> list = majorDirectionService.getMajorDirectionList(listReqVO);
        return success(BeanUtils.toBean(list, MajorDirectionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出专业方向 Excel")
    @PreAuthorize("@ss.hasPermission('ao:major-direction:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMajorDirectionExcel(@Valid MajorDirectionListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<MajorDirectionDO> list = majorDirectionService.getMajorDirectionList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "专业方向.xls", "数据", MajorDirectionRespVO.class,
                        BeanUtils.toBean(list, MajorDirectionRespVO.class));
    }

}